﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using GrewalCRM.Entities;

namespace GrewalCRM.DAL
{
  public class InstallersMaster_DAL : BaseDALGrewalCRM, IBaseDALGrewalCRM
  {
    #region Public  Methods
    public override int AddNew(BaseEntitiesGrewalCRM Entity)
    {
      int result = 0;
      InstallersMaster installer = Entity as InstallersMaster;
      Cmd.CommandText = "usp_InsertInstallersMaster";
      SetParametersForDBSave(installer);
      Cmd.Connection.Open();
      Cmd.ExecuteNonQuery();
      result = (int)(Cmd.Parameters["@result"].Value);
      Cmd.Connection.Close();
      return result;
    }

    public override int Update(BaseEntitiesGrewalCRM Entity)
    {
      int result = 0;
      InstallersMaster installer = Entity as InstallersMaster;
      Cmd.CommandText = "usp_UpdateInstallersMaster";
      SetParametersForDBSave(installer);
      Cmd.Parameters.AddWithValue("@InstallerID", Entity.ID);
      Cmd.Connection.Open();
      Cmd.ExecuteNonQuery();
      result = (int)(Cmd.Parameters["@result"].Value);
      Cmd.Connection.Close();
      return result;
    }

    public override int Delete(BaseEntitiesGrewalCRM Entity)
    {
        int result = 0;
        InstallersMaster installer = Entity as InstallersMaster;
        Cmd.CommandText = "usp_DeleteInstallersMaster";
        SetParametersForDBSave(installer);
        Cmd.Parameters.AddWithValue("@InstallerID", Entity.ID);
        Cmd.Connection.Open();
        Cmd.ExecuteNonQuery();
        result = (int)(Cmd.Parameters["@result"].Value);
        Cmd.Connection.Close();
        return result;
    }

    public override BaseEntitiesGrewalCRM GetByID(int ID)
    {
        Cmd.CommandText = "usp_GetInstallersMasterByID";
        Cmd.Parameters.AddWithValue("@InstallerID", ID);
        Cmd.Connection.Open();
        Rdr = Cmd.ExecuteReader();
        List<BaseEntitiesGrewalCRM> allInstaller = new List<BaseEntitiesGrewalCRM>();

        if (Rdr != null && Rdr.HasRows)
        {
            while (Rdr.Read())
            {


                UserCRM lasrModifiedUser = new UserCRM(Rdr[7] != null && Rdr[7].GetType() != typeof(DBNull) ? Rdr.GetInt32(7) : 0);

                InstallersMaster installer = new InstallersMaster(Rdr[0] != null && Rdr[0].GetType() != typeof(DBNull) ? Rdr.GetInt32(0) : 0,
                                                        Rdr[1] != null && Rdr[1].GetType() != typeof(DBNull) ? Rdr.GetString(1) : string.Empty,
                                                        Rdr[2] != null && Rdr[2].GetType() != typeof(DBNull) ? Rdr.GetString(2) : string.Empty,
                                                        Rdr[3] != null && Rdr[3].GetType() != typeof(DBNull) ? Rdr.GetString(3) : string.Empty,
                                                        Rdr[4] != null && Rdr[4].GetType() != typeof(DBNull) ? Rdr.GetString(4) : string.Empty,
                                                        Rdr[5] != null && Rdr[5].GetType() != typeof(DBNull) ? Rdr.GetDecimal(5) : 0,
                                                        Rdr[6] != null && Rdr[6].GetType() != typeof(DBNull) ? Rdr.GetBoolean(6) : false,
                                                        lasrModifiedUser,
                                                        Rdr[8] != null && Rdr[8].GetType() != typeof(DBNull) ? Rdr.GetDateTime(8) : DateTime.MinValue
                                                       );
                allInstaller.Add(installer);      
            }
        }
        Cmd.Connection.Close();

        if (allInstaller.Count > 0)
            return allInstaller[0];
        else
            return null;
    }
      
   
    public int Save(BaseEntitiesGrewalCRM Entity)
    {
      throw new NotImplementedException();
    }

    public override List<BaseEntitiesGrewalCRM> GetAll()
    {

      Cmd.CommandText = "usp_GetAllInstallersMaster";
      Cmd.Connection.Open();
      Rdr = Cmd.ExecuteReader();
      List<BaseEntitiesGrewalCRM> allInstaller = new List<BaseEntitiesGrewalCRM>();

      if (Rdr != null && Rdr.HasRows)
      {        
        while (Rdr.Read())
        {
            UserCRM lasrModifiedUser = new UserCRM(Rdr[7] != null && Rdr[7].GetType() != typeof(DBNull) ? Rdr.GetInt32(7) : 0);
            lasrModifiedUser.UserName = Rdr[8] != null && Rdr[8].GetType() != typeof(DBNull) ? Rdr.GetString(8) : string.Empty;

            InstallersMaster installer = new InstallersMaster(Rdr[0] != null && Rdr[0].GetType() != typeof(DBNull) ? Rdr.GetInt32(0) : 0,
                                                    Rdr[1] != null && Rdr[1].GetType() != typeof(DBNull) ? Rdr.GetString(1) : string.Empty,
                                                    Rdr[2] != null && Rdr[2].GetType() != typeof(DBNull) ? Rdr.GetString(2) : string.Empty,
                                                    Rdr[3] != null && Rdr[3].GetType() != typeof(DBNull) ? Rdr.GetString(3) : string.Empty,
                                                    Rdr[4] != null && Rdr[4].GetType() != typeof(DBNull) ? Rdr.GetString(4) : string.Empty,
                                                    Rdr[5] != null && Rdr[5].GetType() != typeof(DBNull) ? Rdr.GetDecimal(5) : 0,
                                                    Rdr[6] != null && Rdr[6].GetType() != typeof(DBNull) ? Rdr.GetBoolean(6) : false,
                                                    lasrModifiedUser,
                                                    Rdr[9] != null && Rdr[9].GetType() != typeof(DBNull) ? Rdr.GetDateTime(9) : DateTime.MinValue
                                                   );
            allInstaller.Add(installer); 
        }
      }
      Cmd.Connection.Close();

      return allInstaller;
    }

    public override void FillEntity(BaseEntitiesGrewalCRM Entity)
    {   
        Entity = GetByID(Entity.ID);
    }
    #endregion

    #region Private Method
    private void SetParametersForDBSave(InstallersMaster installer)
    {
      Cmd.Parameters.Clear();
      Cmd.Parameters.Add("@result", System.Data.SqlDbType.Int).Direction = System.Data.ParameterDirection.Output;
     
      if (installer.InstallerName != null && installer.InstallerName.Trim() != string.Empty)
        Cmd.Parameters.AddWithValue("@InstallerName", installer.InstallerName);
      
      if (installer.Address != null && installer.Address.Trim() != string.Empty)
        Cmd.Parameters.AddWithValue("@Address", installer.Address);

      if (installer.PhoneNo != null && installer.PhoneNo.Trim() != string.Empty)
        Cmd.Parameters.AddWithValue("@PhoneNo", installer.PhoneNo);

      if (installer.MobileNo != null && installer.MobileNo.Trim() != string.Empty)
        Cmd.Parameters.AddWithValue("@MobileNo", installer.MobileNo);

       if (installer.InstallerCommission != null && installer.InstallerCommission.HasValue)
        Cmd.Parameters.AddWithValue("@InstallerCommission", installer.InstallerCommission);

      Cmd.Parameters.AddWithValue("@IsActive", installer.IsActive);

      if (null != installer.LastModifiedUser && installer.LastModifiedUser.ID != 0)
        Cmd.Parameters.AddWithValue("@LastModifiedUser", installer.LastModifiedUser.ID);
    }
    #endregion
  }
}
	
